package cn.com.lms.business.livecourse.persistence.repository;

import cn.com.lms.business.livecourse.persistence.entity.CourseLive;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Map;

@Repository
public interface CourseLiveRepository extends BaseRepository<CourseLive, Long> {


//    @Query(value ="SELECT a.id as course_id,a.course_type,a.course_time,b.id as live_id,b.expire_time as live_expire_time FROM bnz_course_info as a left join bnz_course_live as b on a.id=b.course_info_id where " +
//            "a.id =?1", nativeQuery = true)
//    Optional<CourseLiveVo> courseAndLiveByCourseId(Long courseId);

    @Query(value = "SELECT a.id,a.course_info_id as courseInfoId,a.stream_name as streamName,a.push_url as pushUrl,a.pull_url as pullUrl,a.pull_url_flv as pullUrlFlv,a.pull_url_m3u8 as pullUrlM3u8,a.status,a.last_status_update_time as lastStatusUpdateTime,a.vid,a.create_time as createTime,a.expire_time as expireTime,b.tim_group_id as chatRoomGroupId,c.creator_name as creatorName,c.remark,c.course_time as courseTime,c.course_file as courseFile from bnz_course_live a left join bnz_course_chat_room b on a.course_info_id=b.course_info_id left join bnz_course_info c on a.course_info_id=c.id where a.course_info_id=?1 limit 1", nativeQuery = true)
//    CourseLiveVo1 findByCourseId(Long courseId);
    Map<String, Object> findByCourseId(Long courseId);

    @Query(value = "SELECT * from bnz_course_live a where stream_name=?1", nativeQuery = true)
    CourseLive findByStreamName(String streamName);

    @Modifying
    @Query(value = "update bnz_course_live set status=3,vid=?2 where stream_name=?1", nativeQuery = true)
    int vodRecordComplete(String streamName, String vid);

    @Modifying
    @Query(value = "update bnz_course_info set course_time=?2,picture_url=?3 where id=?1", nativeQuery = true)
    int updateLiveCourseInfo(long courseId, long duration, String coverUrl);

    @Query(value = "SELECT d.id FROM bnz_sys_dictionary d where d.type_id =(select id from bnz_sys_dictionary_type dt where dt.code = ?1) and  d.name='直播'", nativeQuery = true)
    String getLiveCourseType(String code);

    @Modifying
    @Query(value = "update bnz_course_info set live_status=?2 where id=?1", nativeQuery = true)
    int modifyCourseLiveStatus(long courseId, int status);

    @Modifying
    @Query(value = "update bnz_course_info set live_status=?2,live_real_start_time=?3 where id=?1", nativeQuery = true)
    int modifyCourseLiveStatusAndTime(long courseId, int status, Date date);

    @Modifying
    @Query(value = "update bnz_course_info set live_status=?2,course_file=?3 where id=(select course_info_id from bnz_course_live where stream_name=?1)", nativeQuery = true)
    int modifyCourseLiveVideoInfoByStreamName(String streamName, int status, String playUrl);

    @Modifying
    @Query(value = "update bnz_course_live set status=?2 where course_info_id=?1", nativeQuery = true)
    int modifyLiveStatus(long courseId, int status);

    @Modifying
    @Query(value = "update bnz_course_live set status=?2 where vid=?1", nativeQuery = true)
    int modifyLiveStatusByVid(String vid, int status);

    @Modifying
    @Query(value = "update bnz_course_info set course_file=?2,course_time=?3,live_status=?4 where id=(select course_info_id from bnz_course_live where vid=?1)", nativeQuery = true)
    int saveCoursePlayUrlByVid(String vid, String playUrl, int duration, int status);

    @Modifying
    @Query(value = "update bnz_course_info set viewers_count=viewers_count+1 where id=?1", nativeQuery = true)
    int incrCourseViewersCount(long courseId);


}
